﻿//登录过程
$(document).ready(function (){
	$("#tologin").on("click",function(){
		var logindialog = layer.open({
	        type: 1
	        ,title: '用户登录'
	        ,closeBtn: false
	        ,area: ['400px', '270px']
	        ,shade: 0.8
	        ,skin: 'layui-layer-molv' //加上边框
	        ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
	        ,btn: ['登录', '取消']
	        ,moveType: 1 //拖拽模式，0或者1
	        ,content: "<div style=padding:5px;margin:0 auto;text-align:center;>"+
	        				"<div class=out>"+
	        					"<form method='post'>"+
		        					"<table>"+
		        						"<tbody>"+
		        							"<tr>"+	
		        								"<th><span>用户名"+"</span></th>"+
		        								"<td><input type=text name=user.username id=username ></input></td>"+
		        								"<td class=tips><a href=''>立即注册</a></td>"+
		        							"</tr>"+
		        							"<tr>"+	
		        								"<th><span>密码"+"</span></th>"+
		        								"<td><input type=password name=user.userpass id=userpass ></input></td>"+
		        								"<td class=tips><a href=''>忘记密码</a></td>"+
		        							"</tr>"+
		        							"<tr>"+	
		        								"<th><span>验证码"+"</span></th>"+
		        								"<td class=yanzheng><input type=text  id=yanzheng ></input></td>"+
		        							"</tr>"+
		        							"<tr>"+	
		        								"<th></th>"+
		        								"<td ><img class=testCode src='/dogstore/imgcode.action' "+
		        								"onclick=changeImg(this) style=cursor: pointer/>&nbsp;&nbsp;&nbsp;点击图片切换</td>"+
		        								
		        							"</tr>"+
		        						"</tbody>"+
		        		   			"</table>"+
		        		   		"</form>"+
	        		   		"</div>"+
	        		   "</div>"
	        ,success: function(layero){
	          var btn = layero.find('.layui-layer-btn');
	          btn.css('text-align', 'center');
	          btn.find('.layui-layer-btn0').attr("href","javascript:");
	          //btn.find('.layui-layer-btn0').attr("onclick","login()");
	          btn.find('.layui-layer-btn0').attr("id","login");
	        }
	        ,yes:function(index,layero){
	        	var username = $("#username").val().trim();
				var userpass = $("#userpass").val().trim();
				var yanzheng = $("#yanzheng").val().trim();
				
				var isnull = true;
				//1.非空校验
				isnull = isNull("username","登录名") && isnull;
				isnull = isNull("userpass","密码") && isnull;
				isnull = isNull("yanzheng","验证码") && isnull;
				
				if(isnull == false){
					return false;
				}else{
					//2.校验验证码
					if(yanzheng != null && yanzheng != ""){
						$.ajax({
								async:false,
								cache:false,
								type:"POST",
								dataType:"json",
								data:{
									valicode:yanzheng
									},
								url:"/dogstore/ajaxValidateCode.action",
								success:function(result){
									if(!result){
										//2-2.验证码错误，不提交
										layer.tips("验证码错误!", "#yanzheng",{tipsMore: true,tips: [2, '#78BA32']});
										return false;
									}else{
										var index = layer.load(2, {time: 3000}); //缓冲3秒
										//3.验证码正确，提交数据
										$.ajax({ 
											type:"post",
											url:"/dogstore/login.action",
											async:false,  
											data:{
												username:username,
												userpass:userpass
											},
											success:function(error){
												if(error == "0"){
													//关闭缓冲
													layer.close(index);
													layer.msg("欢迎登录!",{icon: 1,shift: 2});
													
													//相隔1000毫秒后执行里面的函数
													setTimeout(function (){
														//关闭弹窗
														layer.close(logindialog); 
														//刷新本页面
														window.location.reload();
													},1000);
												}else{
													layer.close(index);
													if(error == "1"){
														layer.msg("账号密码错误",{icon: 2,shift: 6});
													}else{
														if(error == "2"){
															layer.msg("用户没有激活",{icon: 2,shift: 6});
														}else{
															layer.msg("登录失败!-500",{icon: 2,shift: 6});
														}
													}
													return false;
												}
											},error:function(){
												layer.close(index);
												layer.close(logindialog);
												layer.msg("登录提交失败!请联系prince..",{shift: 6});
												return false;
											}
										},"json");	
									}
								}
							});
					 	}
					}
				}
		});
	});
	
	//注销
	$("#loginout").on("click",function(){
		var id = $("#user_id").attr("id");
		$.ajax({
			type:"post",
			dataType:"json",
			data:{
				id:id
				},
			url:"/dogstore/loginout.action",
			success:function(result){
				if(result == "1"){
					//弹出框
					layer.msg("注销成功!",{icon: 1,shift: 1});
					setTimeout(function (){
						//刷新
						window.location.reload();
					},1000);
				}else{
					//弹出框
					layer.msg("注销失败!",{icon: 2,shift: 6});
				}
			},
			error:function(){
				layer.msg("注销失败!请联系prince..",{shift: 6});
			}
		});
	});

    $(document).scroll(function(){
        var top = $(document).scrollTop();
        var tab = $(".headnav");
        if(top > 0){
            $(".headnav").css("position","fixed");
            $(".headnav").css("width","100%");
        }else if(top <= 0){
        	$(".headnav").css("position","relative");
        	$(".headnav").css("width","100%");
        }
    });
});


function isNull(name,error){
    var isnull = true;
    var value = $("#"+name).val().trim();
	if(value == "" || value == null){
			
		layer.tips(error+"不能为空!", "#"+name,{tipsMore: true,tips: [2, '#78BA32']});
		isnull = false;
	}
	return isnull;
}

function changeImg(img){
	img.src = img.src+"?time="+new Date().getTime();
}
